//背景对象
var backGround = backGround || {};


backGround.init = function(){
	backMaterial = o3djs.material.createMaterialFromFile(cubePack,
															 materialUrls[0],
															 backViewInfo.zOrderedDrawList);
	backMaterial.getParam("colorMult").value = [1, 1, 1, 1];
	backMaterial.drawList = backViewInfo.zOrderedDrawList;
	shape[5] = o3djs.primitives.createPlane(cubePack,
												backMaterial,
												1,
												1,
												1,
												1,
												[[1, 0, 0, 0],
												[0, 0, 1, 0],
												[0,-1, 0, 0],
												[0, 0, 0, 1]]);
	
	var repectTexture = backGround.createTexture([[223/255,230/255,1,1],[213/255,223/255,1,1]],10,1024,"upToDown");
	//var repectTexture = backGround.createTexture([1,1,1,1],10,1024);
	for(var n=0;n < 200;n++){
		backImage[n] = new backGround.frame("chips0",[],[repectTexture],[],[]);
		backImage[n].moveTo(n*10,0);
		backImage[n].changeZ(-500);
		}
	}
	
backGround.createTexture = function(color,width,height,direction){
	
			var bgColorTexture = cubePack.createTexture2D(
														 width,
														 height,
														 o3djs.base.o3d.Texture.ARGB8,
														 1,
														 false);
			
			var linearPaint = cubePack.createObject('CanvasPaint');

			
			var bgImageCanvas = cubePack.createObject("Canvas");
			bgImageCanvas.setSize(width,height);
			
			if(color[0][0] != undefined){
				bgImageCanvas.clear([0,0,0,0]);
				var gradientShader = cubePack.createObject("CanvasLinearGradient");
				switch(direction){
					case "leftToRight":
						gradientShader.startPoint = [-1, 0];
						gradientShader.endPoint = [width-1, 0];
						break;
					case "rightToLeft":
						gradientShader.startPoint = [width-1, 0];
						gradientShader.endPoint = [-1, 0];
						break;
					case "downToUp":
						gradientShader.startPoint = [0, height-1];
						gradientShader.endPoint = [0, -1];
						break;
					case "upToDown":
						gradientShader.startPoint = [0, 0];
						gradientShader.endPoint = [0, height];
						break;
					default :
						gradientShader.startPoint = [0, 0];
						gradientShader.endPoint = [width, 0];
						break;
					}
					gradientShader.colors = color;
					gradientShader.positions = [];
					gradientShader.tileMode = cubeO3d.CanvasShader.REPEAT;
				linearPaint.shader = gradientShader;
				bgImageCanvas.drawRect(0,0,width,height,linearPaint);
				}
			else{
				bgImageCanvas.clear(color);
				}
			bgColorTexture.drawImage(bgImageCanvas,0,height-1,width,-height,
									   0,0,0,width,height
									   );	
			return bgColorTexture;
	}

	
	
backGround.frame = function(name,text,bgImage,bgColor,activation,z_index){
	this.name = name;
	this.x = 0;
	this.y = 0;
	this.z = z_index == undefined ? 0 : z_index;
	
	this.textContent = text[0];
	this.textCoordinateX = text[1] ? text[1] : 0;
	this.textCoordinateY = text[2] ? text[2] : 0;
	this.textTypeface = text[3] ? text[3] : "Microsoft YaHei";
	this.textSize = text[4] ? text[4] : 10;
	this.textColor = text[5] ? text[5] : [0,0,0,1];
	this.limitWidth = text[6] ? text[6] : 99999999;
	
	this.bgImageTexture = bgImage[0];
	this.bgImageTextureAlpha = bgImage[1] != undefined ? bgImage[1] : 1;
	this.bgImageWidth = bgImage[2];
	this.bgImageHeight = bgImage[3];
	
	this.bgColor = bgColor[0];
	this.bgWidth = bgColor[1];
	this.bgHeight = bgColor[2];
	this.bgDirection = bgColor[3];
	
	if(activation["activeDown"]){
		this.activeDown = activation["activeDown"];
		}
		else{
			this.activeDown = function(){};
			}
			
	if(activation["activeOver"]){
		this.activeOver = activation["activeOver"];
		}
		else{
			this.activeOver = function(){};
			}
			
	if(activation["activeUp"]){
		this.activeUp = activation["activeUp"];
		}
		else{
			this.activeUp = function(){};
			}
	if(activation["activeDrag"]){
		this.activeDrag = activation["activeDrag"];
		}
		else{
			this.activeDrag = function(){};
			}
	if(activation["activeLeave"]){
		this.activeLeave = activation["activeLeave"];
		}
		else{
			this.activeLeave = function(){};
			}
	
	if(activation["defultFunction"]){
		this.defultFunction = activation["defultFunction"];
		}
		else{
			this.defultFunction = function(){};
		}
			
	this.transform = cubePack.createObject("Transform");
	this.transform.parent = backImage.transform;
	this.scaleTransform = cubePack.createObject("Transform");
	this.scaleTransform.parent = this.transform;
	
	this.transform.translate(0,0,this.z);
	//----------------------------------------------------------------------
	if(this.bgImageTexture){
		this.bgImageMoveTransform = cubePack.createObject("Transform");
		this.bgImageMoveTransform.parent = this.scaleTransform;
		
		this.bgImageTransform = cubePack.createObject("Transform");
		this.bgImageTransform.parent = this.bgImageMoveTransform;
		
		this.bgImageSampler = cubePack.createObject('Sampler');
		this.bgImageSampler.addressModeU = cubeO3d.Sampler.CLAMP;
		this.bgImageSampler.addressModeV = cubeO3d.Sampler.CLAMP;
		
		this.bgImageParamSampler = this.bgImageTransform.createParam('texSampler0',
														'ParamSampler');
		this.bgImageParamSampler.value = this.bgImageSampler;
		this.bgImageParamColorMult = this.bgImageTransform.createParam('colorMult',
														'ParamFloat4');
		
		this.bgImageParamColorMult.set(1, 1, 1, this.bgImageTextureAlpha);
		
		this.bgImageSampler.texture = this.bgImageTexture;
		this.bgImageTransform.addShape(shape[5]);	
		
			if(this.bgImageWidth && this.bgImageHeight){
				this.width = this.bgImageWidth;
				this.height = this.bgImageHeight;
				}
			else{
				this.width = this.bgImageTexture.width;
				this.height = this.bgImageTexture.height;
				}
		this.bgImageTransform.translate(this.width/2,this.height/2,0);
		this.bgImageTransform.scale(this.width,-this.height,1);
		
		//this.bgImageMoveTransform.translate(this.bgImageCoordinateX,this.bgImageCoordinateX,0);
		}
		else if(this.bgColor  && this.bgColor[0] != undefined){
			this.width = this.bgWidth;
			this.height = this.bgHeight;
			
			this.bgImageMoveTransform = cubePack.createObject("Transform");
			this.bgImageMoveTransform.parent = this.scaleTransform;
			
			this.bgImageTransform = cubePack.createObject("Transform");
			this.bgImageTransform.parent = this.bgImageMoveTransform;
			
			this.bgColorTexture = cubePack.createTexture2D(
														 this.width,
														 this.height,
														 o3djs.base.o3d.Texture.ARGB8,
														 1,
														 false);
			
			var linearPaint = cubePack.createObject('CanvasPaint');

			
			this.bgImageCanvas = cubePack.createObject("Canvas");
			this.bgImageCanvas.setSize(this.width,this.height);
			
			if(this.bgColor[1][0] != undefined){
				this.bgImageCanvas.clear([0,0,0,0]);
				var gradientShader = cubePack.createObject("CanvasLinearGradient");
				switch(this.bgDirection){
					case "leftToRight":
						gradientShader.startPoint = [-1, 0];
						gradientShader.endPoint = [this.width-1, 0];
						break;
					case "rightToLeft":
						gradientShader.startPoint = [this.width-1, 0];
						gradientShader.endPoint = [-1, 0];
						break;
					case "downToUp":
						gradientShader.startPoint = [0, this.height-1];
						gradientShader.endPoint = [0, -1];
						break;
					case "upToDown":
						gradientShader.startPoint = [0, 0];
						gradientShader.endPoint = [0, this.height];
						break;
					default :
						gradientShader.startPoint = [0, 0];
						gradientShader.endPoint = [this.width, 0];
						break;
					}
					gradientShader.colors = this.bgColor;
					gradientShader.positions = [];
					gradientShader.tileMode = cubeO3d.CanvasShader.REPEAT;
				linearPaint.shader = gradientShader;
				this.bgImageCanvas.drawRect(0,0,this.width,this.height, linearPaint);
				}
			else{
				this.bgImageCanvas.clear(this.bgColor);
				}
			this.bgColorTexture.drawImage(this.bgImageCanvas,0,this.height-1,this.width,-this.height,
									   0,0,0,this.width,this.height
									   );
			this.bgImageSampler = cubePack.createObject('Sampler');
			this.bgImageSampler.addressModeU = cubeO3d.Sampler.CLAMP;
			this.bgImageSampler.addressModeV = cubeO3d.Sampler.CLAMP;
			
			this.bgImageParamSampler = this.bgImageTransform.createParam('texSampler0',
															'ParamSampler');
			this.bgImageParamSampler.value = this.bgImageSampler;
			this.bgImageParamColorMult = this.bgImageTransform.createParam('colorMult',
															'ParamFloat4');
			this.bgImageParamColorMult.set(1, 1, 1, 1);
				
			this.bgImageSampler.texture = this.bgColorTexture;
			this.bgImageTransform.addShape(shape[5]);	
			
			this.bgImageTransform.translate(this.width/2,this.height/2,0);
			this.bgImageTransform.scale(this.width,-this.height,1);
			//this.bgImageMoveTransform.translate(this.bgImageCoordinateX,this.bgImageCoordinateX,0);
			}
			else if(this.bgColor == undefined){
				this.width = this.bgWidth;
				this.height = this.bgHeight;
				}
	//========================================================================
	if(this.textContent){
		this.textMoveTransform = cubePack.createObject("Transform");
		this.textMoveTransform.parent = this.scaleTransform;
		
		this.textTransform = cubePack.createObject("Transform");
		this.textTransform.parent = this.textMoveTransform;
		
		
		this.textPaint = cubePack.createObject("CanvasPaint");
		this.textPaint.color = this.textColor;
		this.textPaint.textSize = this.textSize;
		this.textPaint.textTypeface = this.textTypeface;
		var fontBox = this.textPaint.measureText(this.textContent);
		this.textWidth = fontBox[2]-fontBox[0];
		this.textHeight = fontBox[3]-fontBox[1];
			if(this.textWidth>this.limitWidth){
				//按合适的宽度拆分字符串，保存到splitText（一个数组）
				var writeInRows = true;
				var splitText = dialog.splitTextBy(this.textPaint,this.textContent,this.limitWidth);
				fontBox = this.textPaint.measureText(splitText[0]);
				this.textWidth = this.limitWidth;
				this.textHeight = (fontBox[3]-fontBox[1]+3)*splitText.length;
				}
		
		if (this.textHeight%2 != 0){this.textHeight = this.textHeight +1;}
		if (this.textWidth %2 != 0){this.textWidth = this.textWidth +1;}
		
		this.textTexture = cubePack.createTexture2D(
													 this.textWidth,
													 this.textHeight,
													 o3djs.base.o3d.Texture.ARGB8,
													 1,
													 false);
		this.textCanvas = cubePack.createObject("Canvas");
		this.textCanvas.setSize(this.textWidth,this.textHeight);
		this.textCanvas.clear([1,1,1,0]);
		this.textTexture.drawImage(this.textCanvas,0,this.textHeight-1,this.textWidth,-this.textHeight,
								   0,0,0,this.textWidth,this.textHeight
								   );
		//根据暂存开关writeInRows来进入分支
			if(writeInRows){
				for(var nRows=0;nRows < splitText.length;nRows++){
					this.textCanvas.drawText(splitText[nRows],
										 -fontBox[0],
										 (fontBox[3]-fontBox[1])*(nRows+1)-fontBox[3]+nRows*3,
										 this.textPaint);
					}
				}
			else{
				this.textCanvas.drawText(this.textContent,
									 -fontBox[0],
									 this.textHeight-fontBox[3],
									 this.textPaint);
				}
		
		this.textTexture.drawImage(this.textCanvas,0,this.textHeight-1,this.textWidth,-this.textHeight,
								   0,0,0,this.textWidth,this.textHeight
								   );
		this.textSampler = cubePack.createObject('Sampler');
		this.textSampler.addressModeU = cubeO3d.Sampler.CLAMP;
		this.textSampler.addressModeV = cubeO3d.Sampler.CLAMP;
		
		this.textParamSampler = this.textTransform.createParam('texSampler0',
														'ParamSampler');
		this.textParamSampler.value = this.textSampler;
		this.textParamColorMult = this.textTransform.createParam('colorMult',
														'ParamFloat4');
		
		this.textParamColorMult.set(1, 1, 1, 1);
		
		this.textSampler.texture = this.textTexture;
		this.textTransform.addShape(shape[5]);	
		
		this.textTransform.translate(this.textWidth/2,this.textHeight/2,0.001);
		this.textTransform.scale(this.textWidth,-this.textHeight,1);
		
		if (!this.width){
			this.width = this.textWidth;
		}
		if (!this.height){
			this.height = this.textHeight;
 			}
		
		if (this.textCoordinateX  == "left"){
			this.textMoveTransform.translate(3,0,0);
			}else if (this.textCoordinateX  == "center"){
				this.textMoveTransform.translate(Math.round((this.width - this.textWidth - 3)/2),0,0);
				}else if(this.textCoordinateX  == "right"){
					this.textMoveTransform.translate(this.width - this.textWidth,0,0);
					}else {
						this.textMoveTransform.translate(this.textCoordinateX,0,0);
						}
		if (this.textCoordinateY  == "top"){
			this.textMoveTransform.translate(0,3,0);
			}else if (this.textCoordinateY  == "middle"){
				this.textMoveTransform.translate(0,Math.round((this.height - this.textHeight)/2),0);
				}else if(this.textCoordinateY  == "bottom"){
					this.textMoveTransform.translate(0,this.height - this.textHeight - 3,0);
					}else{
						this.textMoveTransform.translate(0,this.textCoordinateY,0);
						}
		}
	}

backGround.frame.prototype.moveTo = function(x,y){
	this.x = x;
	this.y = y;
	this.transform.identity();
	this.transform.translate(x,y,this.z);
	
	}
	
backGround.frame.prototype.changeZ = function(z){
	this.z = z;
	this.transform.identity();
	this.transform.translate(this.x,this.y,z);
	}
	
